/*******************************************************************************
*LAST EDITED: 02/06/2022
*CONTRIBUTORS: BC, MEK & AO
*PROJECT: Is It Who You Are or What You Get? Comparing the Impacts of Loans 
		  and Grants for Microenterprise Development
*JOURNAL: American Economic Journal-Applied Economics
*DESCRIPTION: This program installs all required packages and dependencies. 
			  This program takes raw data and converts it into clean datasets.
			  It must be run prior to any other analysis.
*******************************************************************************/
	clear all
	set rmsg on
	set seed 12345
	set maxvar 32767  // Modify if not StataMP 
	set max_memory . // (.) operating system  willing to supply all memory
	set more off
	set matsize 10000
	set scheme s2color
	cap ssc install winsor2
	grstyle init
	grstyle set plain, horizontal grid
	
*Global 
	*Root 
			*global root "c\tmp\" // Include and update this line if you're running this do file individually and not through "00_SetUp.do"
			global program "$root"
		
			global clean "$program\Do Files"
			global analysis "$program\Do Files"
			global appendix "$program\Do Files\Appendix"
			global data "$root"
		
		*Branch 
			global rdata "$data\Data"
			global adata "$data\Do Files"
********************************************************************************
/*
INPUT: The program requires 3 datasets 
	1) AEJ_CKO_baseline.dta
	2) AEJ_CKO_endline.dta
OUTPUT: The program generates 3 datasets required for the main analysis
	1) AEJ_CKO_baseline_edited.dta
	2) AEJ_CKO_endline_edited.dta
	3) AEJ_CKO_baseline_endline_edited.dta
*/		
	
		***********************************************************
		***********************************************************
						   ***DATA CLEANING***
		***********************************************************
		***********************************************************
	
********************************************************************************	
*****1) Baseline to Baseline Edited 
********************************************************************************
	use "$rdata\AEJ_CKO_baseline.dta",clear 
	*Clean variables
		*Gender 
			gen female=0
			replace female=1 if gender_rand==0
			gen male=0
			replace male=1 if gender_rand==1
		*Treatment 
			gen Loan=treatment==1
			gen In_Kind=treatment==2
			gen Cash=treatment==3
			label var Loan "Micro credit"
			label var In_Kind "In kind grant"
			label var Cash "Cash grant"

			capture drop Capital
			gen Capital=Loan+In_Kind+Cash

			gen LoanF=Loan*female
			gen In_KindF=In_Kind*female
			gen CashF=Cash*female
			label var LoanF "Micro credit"
			label var In_KindF "In kind grant"
			label var CashF "Cash grant"

			gen LoanM=Loan*male
			gen In_KindM=In_Kind*male
			gen CashM=Cash*male
			label var LoanM "Micro credit"
			label var In_KindM "In kind grant"
			label var CashM "Cash grant"

			capture drop Control
			gen Control=treatment==0
			
			capture drop loanR
			gen loanR=Loan*(amount_received>0)
			capture drop CashR
			gen CashR=Cash*(amount_received>0)
			capture drop In_KindR
			gen In_KindR=In_Kind*(amount_received>0)

		*20 errors in admin data invidiually confirmed with partners.
			gen amount_received_corrected= amount_received
			replace amount_received_corrected=0 if treatment==0
			gen amount_received_condition=amount_received_corrected if received_yesno==1
		
		*Treatment short indicators 
						
			*Loans
				gen treat_loan=1 if inlist(treatment_status,1)
				replace treat_loan=0 if inlist(treatment_status,0,2,3)
			*In-kind
				gen treat_inkind=1 if inlist(treatment_status,2)
				replace treat_inkind=0 if inlist(treatment_status,0,1,3)
			*Cash Grant
				gen treat_cashgrant=1 if inlist(treatment_status,3)
				replace treat_cashgrant=0 if inlist(treatment_status,0,1,2)
		
		*Amount requested
			destring loan_capital, replace
		
		*Educ Categories
			gen educ_college=1 if inlist(edu,88)
			replace educ_college=0 if inlist(edu, 11,22,33,44,55,66,77)

			gen educ_somecollege=1 if inlist(edu,77)
			replace educ_somecollege=0 if inlist(edu, 11,22,33,44,55,66,88)

			gen educ_hs=1 if inlist(edu, 66)
			replace educ_hs=0 if inlist(edu, 11,22,33,44,55,88,77)

			gen educ_lths=1 if inlist(edu, 11,22,33,44,55)
			replace educ_lths=0 if inlist(edu, 88,66,77)
		*Age	
			gen agebase=2017-birth_year
		*Marital status
			gen marital_s=1 if inlist(marital,1)
			replace marital_s=0 if inlist(marital, 2,3,4)

			gen marital_m=1 if inlist(marital, 2)
			replace marital_m=0 if inlist(marital, 1,3,4)
		*Previous employment
			recode worked_before (2=0 "No") (1=1 "Yes"), pre(new)
			replace newworked_before=0 if newworked_before==.
			replace newworked_before=0 if newworked_before==99
		*Has a business
			gen has_biz= 0
			replace has_biz= 1 if inlist(emp_type,"3","4")
		*Family income
			gen family_inc_low=1 if inlist(family_inc,1,2)
			replace family_inc_low=0 if inlist(family_inc,3,4,5)

			gen family_inc_mid=1 if inlist(family_inc,3,4,5)
			replace family_inc_mid=0 if inlist(family_inc,1,2)
		*Family size
			gen size_low=1 if child_count<=4
			replace size_low=0 if child_count>4 & child_count<=15

			gen size_high=1 if child_count<=15
			replace size_high=0 if child_count<5
		*Borrowing
			gen formal_loan=0
			replace formal_loan=1 if inlist(last_loan_source,1,2)

			gen family_loan=0
			replace family_loan=1 if last_loan_source==3

			gen rosca=0
			replace rosca=1 if last_loan_source==4

			gen any_borrowing=0
			replace any_borrowing=1 if loan_fund==1
		*Migration
			gen wants_migrate=0
			replace wants_migrate=1 if migrate_yesno==1
		*Training
			gen trained_yesno=0 if training_type==0 & inlist(treatment_status, 1,2,3)
			replace trained_yesno=1 if inlist(training_type,1,2,3) & inlist(treatment_status, 1,2,3)
		*Has kids
			gen has_kids=0
			replace has_kids=1 if child==1
		*Broad external pressure
			rename successful_requests_money success_req_money
			foreach var of varlist profit_family money_family_request success_req_money machines_save_money house_mates marital_m {
				recode `var' (2=0) (99=.)
			}

			replace house_mates=. if house_mates>15

			foreach var of varlist profit_family money_family_request success_req_money machines_save_money house_mates marital_m {
				replace `var'=0 if `var'==.
				sum `var' if treatment==0 & baseline_gender==0
				scalar define `var'_ctrl_m_m=r(mean)
				scalar define `var'_ctrl_sd_m=r(sd)
				sum `var' if treatment==0 & baseline_gender==1
				scalar define `var'_ctrl_m_f=r(mean)
				scalar define `var'_ctrl_sd_f=r(sd)
				gen m_`var'=`var'_ctrl_m_m
				replace m_`var'= `var'_ctrl_m_f if baseline_gender==1
				gen sd_`var'=`var'_ctrl_sd_m
				replace sd_`var'= `var'_ctrl_sd_f if baseline_gender==1
				gen std_`var' = (`var' - m_`var') / sd_`var'
			}
			*Broad external pressure Index
				egen broad_index=rowtotal(std_profit_family std_money_family_request std_success_req_money std_machines_save_money std_house_mates std_marital_m)
				sum broad_index if treatment==0 & baseline_gender==1
				scalar define broad_index_mean_f=r(mean)
				scalar define broad_index_sd_f=r(sd)
				sum broad_index if treatment==0 & baseline_gender==0
				scalar define broad_index_mean_m=r(mean)
				scalar define broad_index_sd_m=r(sd)
				gen m_broad_index= broad_index_mean_f
				replace m_broad_index= broad_index_mean_m if baseline_gender==0
				gen sd_broad_index= broad_index_sd_f
				replace sd_broad_index= broad_index_sd_m if baseline_gender==0
				gen std_broad_pressure = ( broad_index - m_broad_index ) / sd_broad_index
		*Received training
			gen received_train=0
			replace received_train=1 if training_type!=0
			replace received_train=0 if treatment_status==0
		*Treated v. non treated attriters
			gen treated=0
			replace treated=1 if inlist(treatment,1,2,3)
		*Save 
			ren * b_*
			ren b_u_key u_key
			compress
			save "$rdata/AEJ_CKO_baseline_edited.dta",replace

********************************************************************************
*****2) Endline Edited 
********************************************************************************
	use "$rdata\AEJ_CKO_endline.dta",clear 
	*Clean variables
	
	drop if confirm_consent==0
		g treatment=treatment_status
		g female=gender==1
		g male=gender==0
		xi i.treatment*i.female, pre(T_)
		xi i.treatment*i.male, pre(S_)
		g b_round=round

*Treatment 
			gen Loan=treatment==1
			gen In_Kind=treatment==2
			gen Cash=treatment==3
			label var Loan "Micro credit"
			label var In_Kind "In kind grant"
			label var Cash "Cash grant"

			capture drop Capital
			gen Capital=Loan+In_Kind+Cash

			gen LoanF=Loan*female
			gen In_KindF=In_Kind*female
			gen CashF=Cash*female
			label var LoanF "Micro credit"
			label var In_KindF "In kind grant"
			label var CashF "Cash grant"

			gen LoanM=Loan*male
			gen In_KindM=In_Kind*male
			gen CashM=Cash*male
			label var LoanM "Micro credit"
			label var In_KindM "In kind grant"
			label var CashM "Cash grant"
			capture drop Control
			gen Control=treatment==0

	
*********other borrowing******
foreach var of varlist rosca_startdate_1 rosca_startdate_2 rosca_startdate_3 rosca_startdate_4 familyloan_when_received_1 familyloan_when_received_2 familyloan_when_received_3 familyloan_when_received_4 familyloan_when_received_5 familyloan_when_received_6 familyloan_when_received_7 familyloan_when_received_8 loan_when_received_1 loan_when_received_2 loan_when_received_3 loan_when_received_4{
format `var' %tdNN/DD/CCYY
}

*Feda**
replace date_received="1/23/2018" if date_received=="23jan2018"
replace date_received="7/2/2018" if date_received=="02jul2018"
replace date_received="1/2/2018" if date_received=="02jan2018"
replace date_received="12/2/2018" if date_received=="02dec2018"
replace date_received="1/29/2018" if date_received=="29jan2018"
replace date_received="12/26/2017" if date_received=="26/12/2017"
replace date_received="12/14/2017" if date_received=="14/12/2017"
replace date_received="12/20/2017" if date_received=="20/12/2017"
replace date_received="12/18/2017" if date_received=="18/12/2017"

**redec**
replace date_received="12/25/2016" if date_received=="25/12/2016"
replace date_received="11/8/2016" if date_received=="0811/2016"
replace date_received="9/17/2017" if date_received=="17/09/2017"
replace date_received="2/20/2017" if date_received=="20/02/2017"
replace date_received="12/21/2017" if date_received=="21/12/2017"
replace date_received="8/22/2017" if date_received=="22/08/2017"
replace date_received="4/23/2017" if date_received=="23/04/2017"
replace date_received="8/23/2017" if date_received=="23/08/2017"
replace date_received="5/23/2017" if date_received=="23/05/2017"
replace date_received="3/26/2017" if date_received=="26/03/2017"
replace date_received="6/5/2017" if date_received=="5\6\2017"
replace date_received="6/6/2017" if date_received=="6\6\2017"

**Christian peace**
replace date_received="4/9/2017" if date_received=="09/04/2017"
replace date_received="4/8/2017" if date_received=="08/4/2017"
replace date_received="4/10/2017" if date_received=="10/04/2017"
replace date_received="4/11/2017" if date_received=="11/04/2017"
replace date_received="4/12/2017" if date_received=="12/04/2017"
replace date_received="12/17/2017" if date_received=="17/12/2017"
replace date_received="4/18/2017" if date_received=="18/04/2017"
replace date_received="12/28/2017" if date_received=="28/12/2017"


gen received_date=date( date_received , "MDY")
format received_date %tdNN/DD/CCYY

gen match_date=0
foreach var of varlist loan_when_received_1 loan_when_received_2 loan_when_received_3 loan_when_received_4{
replace match_date=1 if `var'==received_date
}
replace match_date=. if received_date==.

gen match_amount=0
foreach var of varlist loan_amount_1 loan_amount_2 loan_amount_3 loan_amount_4{
replace match_amount=1 if `var'==amount_received
}
replace match_amount=. if amount_received==0


gen date_difference= loan_when_received_1-received_date
gen date_difference_2= loan_when_received_2-received_date
gen date_difference_3= loan_when_received_3-received_date
gen date_difference_4= loan_when_received_4-received_date

gen matched_loan=0 
replace matched_loan=1 if match_amount==1 & abs(date_difference)<=66
replace matched_loan=1 if match_amount==1 & abs(date_difference_2)<=66
replace matched_loan=1 if match_amount==1 & abs(date_difference_3)<=66
replace matched_loan=1 if match_amount==1 & abs(date_difference_4)<=66

**********Primary Outcomes******************** 
destring calculate_expenses, replace
replace monthly_profits=correct_profits if correct_profits!=.
replace monthly_revenue=correct_sales if correct_sales!=.

g e_hasbiz=1
replace e_hasbiz=0 if why_no_project!=.
replace e_hasbiz=. if income_sources==""

foreach var of varlist  days_wageemp days_selfemp hours_wageemp hours_selfemp{
replace `var'=0 if `var'==.
}

g e_days_s=days_selfemp
g e_hours_s=hours_selfemp
g e_weekly_hours_s=e_days_s*e_hours_s

g e_employees=staff_yesno==1
replace e_employees=1 if staff_yesno_other==1


g e_days_w=days_wageemp
g e_hours_w=hours_wageemp
g e_weekly_hours_w=e_days_w*e_hours_w

g e_weekly_hours=e_weekly_hours_s+e_weekly_hours_w
replace e_weekly_hours=112 if e_weekly_hours>112
*not allowing people to work more than 16 hours a day, 7 days a week
gen e_monthly_hours= e_weekly_hours*4

gen weekly_hours=e_weekly_hours
replace weekly_hours=. if e_weekly_hours==0

g e_works_for_others=e_weekly_hours_w!=0
g e_works_at_all=e_works_for_others+e_hasbiz
replace e_works_at_all=1 if e_works_at_all==2
g e_notworking=e_weekly_hours==0

egen income_activities= rowtotal(income_sources_1 income_sources_2 income_sources_3)
gen biz_wage=0
replace biz_wage=1 if e_hasbiz==1 & e_works_for_others==1

*constructed profits = profits - expenses
g e_expenses= calculate_expenses
replace e_expenses= correct_project_expenses_total if correct_project_expenses_total!=.
g e_cp=monthly_revenue-e_expenses

foreach var of varlist monthly_profits monthly_revenue e_expenses e_cp wage_last_month{
replace `var'=0 if `var'==.
replace `var'=. if `var'==9999
replace `var'=. if `var'==999
gen ihs_`var'= ln(`var'+ sqrt(`var'^2+1))
winsor2 `var' if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
winsor2 `var' if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
egen `var'_w=rowtotal(`var'_f `var'_m)
}

egen biz_inc= rowtotal(income_project)
egen wage_inc= rowtotal( wage_month )
egen transfers_inc= rowtotal( received_transfers_value )
egen rent_inc= rowtotal( rent_asset )
egen govt_inc= rowtotal( received_govsupport_value )
egen other_inc= rowtotal( other_income )

gen work_inc = biz_inc + wage_inc
gen hourly_wage_monthly= work_inc/ e_monthly_hours
replace hourly_wage_monthly=0 if hourly_wage_monthly==.

foreach var of varlist *_inc{
replace `var'=. if `var'==9999
replace `var'=. if `var'==999
winsor2 `var' if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
winsor2 `var' if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
egen `var'_w=rowtotal(`var'_f `var'_m)
}

g e_total_income=monthly_profits+wage_inc+rent_inc+other_inc+govt_inc+transfers_inc
winsor2 e_total_income if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
winsor2 e_total_income if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
egen e_total_income_w=rowtotal(e_total_income_f e_total_income_m)

*g e_total_income_w=monthly_profits_w+wage_inc_w+rent_inc_w+other_inc_w+govt_inc_w+transfers_inc_w
g e_wage=e_total_income/e_weekly_hours

*ihstrans e_total_income*
gen ihs_e_total_income = ln(e_total_income+ sqrt(e_total_income^2+1))

**loans vs. combined grants**
gen treatment_combined=0 if treatment==0
replace treatment_combined=1 if treatment==1
replace treatment_combined=2 if inlist(treatment,2,3)

**************male vs. female**********
gen treat_loan=1 if inlist(treatment_status,1)
replace treat_loan=0 if inlist(treatment_status,0,2,3)

*In-kind
gen treat_inkind=1 if inlist(treatment_status,2)
replace treat_inkind=0 if inlist(treatment_status,0,1,3)

*Cash Grant
gen treat_cashgrant=1 if inlist(treatment_status,3)
replace treat_cashgrant=0 if inlist(treatment_status,0,1,2)

*Loans
gen loan_f=female*treat_loan
gen loan_m=male*treat_loan

*In-kind
gen inkind_f=female*treat_inkind
gen inkind_m=male*treat_inkind

*Cash Grant
gen cash_f=female*treat_cash
gen cash_m=male*treat_cash

**Gender**
xi i.treatment_combined*i.female, pre(M_)
xi i.treatment_combined*i.male, pre(W_)

**************male vs. female**********
gen treated_loan=1 if inlist(treatment_combined,1)
replace treated_loan=0 if inlist(treatment_combined,0,2)

gen treated_grant=1 if inlist(treatment_combined,2)
replace treated_grant=0 if inlist(treatment_combined,0,1)

*Loans
gen loan_fe=female*treated_loan
gen loan_ma=male*treated_loan

*grants
gen grants_f=female*treated_grant
gen grants_m=male*treated_grant

************************treatment on treated***********************************
gen take_up=0
replace take_up=1 if received_yesno==1 & treatment==1
replace take_up=1 if received_yesno==1 & treatment==0
replace take_up=3 if received_yesno==1 & treatment==3
replace take_up=2 if received_yesno==1 & treatment==2

/**********Secondary Outcomes***************/

foreach var of varlist *_wealth_count saved_amount{
replace `var'=0 if `var'==.
egen m_`var' = mean(`var'), by(gender)
egen sd_`var' = sd(`var'), by(gender)
gen std_`var' = (`var' - m_`var') / sd_`var'
}

egen total_wealth=rowtotal(std_*wealth_count saved_amount)
egen std_wealth_f=std(total_wealth) if gender==1
egen std_wealth_m=std(total_wealth) if gender==0
egen std_wealth=rowtotal(std_wealth_f std_wealth_m)
*This is a measure of household wealth

gen gt=treatment
replace gt=gt+10 if female==1

*The following are measures of business assets
egen new_biz_assets=rowtotal(inventory_amount goods_amount cash_amount premise_new land_new furniture_new equipment_machinery_new vehicules_new amount_maintanance)
egen all_biz_assets=rowtotal(inventory_amount goods_amount cash_amount premise_new land_new furniture_new equipment_machinery_new vehicules_new amount_maintanance premise land furniture equipment_machinery vehicules)
ihstrans *_biz_assets
winsor2 new_biz_assets, cuts(0 99) by(gt)
winsor2 all_biz_assets, cuts(0 99) by(gt)

*HH consumption
egen hh_year=rowtotal(clothing_expenses footwear_expenses mobile_expenses other_large_expenses)
egen hh_quarter=rowtotal(events_expenses electronics_expenses furniture_expenses vehicles_expenses health_expenses othereducation_expenses)
egen hh_month=rowtotal(water_expenses personalitems_expenses personalservices_expenses cleaning_expenses transportation_expenses communication_expenses entertainment_expenses other_expenses hhservices_expenses contributions_expenses)
egen hh_week=rowtotal(bread_expenses vegetables_expenses fruits_expenses chicken_expenses dairy_expenses tea_expenses potablewater_expenses)
gen hh_total=hh_year/12+hh_quarter/3+hh_month+hh_week*4

winsor2 hh_total, cuts(0 99) by(gt)
ihstrans hh_total

*Making Higher number "better"
recode health_description (1=5) (2=4) (4=2) (5=1)

*gen mental health index
foreach var of varlist worried_yesno depressed{
replace `var'=. if `var'==6
egen std_`var'_f=std(`var') if gender==1
egen std_`var'_m=std(`var') if gender==0
egen std_`var'=rowtotal (std_`var'_f std_`var'_m)
}

gen worried_days=31-peaceful_lastmonth_days
egen std_worried_f=std(worried_days) if gender==1
egen std_worried_m=std(worried_days) if gender==0
egen std_worried=rowtotal(std_worried_f std_worried_m)

gen mental_health=std_worried_yesno+std_depressed+std_worried
egen std_mental_health_f=std(mental_health) if gender==1
egen std_mental_health_m=std(mental_health) if gender==0
egen std_mental_health=rowtotal(std_mental_health_f std_mental_health_m)
replace std_mental_health=-1*std_mental_health
**Making bad mental health a negative value.

***Decision power***
g dec_working= inlist(working_household_decision,1,2,5)
g dec_daily= inlist(daily_household_decision,1,2,5)
g dec_fin= inlist( degree_financial_household_decis ,1,2,3)

egen dec_power=rowtotal(dec*)

***Time worked at home***
gen startdate=dofc(starttime)
foreach var of varlist agri_home_time errands_home_time maintenance_home_time cleaning_home_time fuel_home_time childcare_home_time other_childcare_home_time{
replace `var'=0 if `var'==. & startdate>=td(01oct2018)
winsor2 `var', cuts(0 99) by(gt)
}

egen home_working_hrs =rowtotal(agri_home_time_w errands_home_time_w maintenance_home_time_w cleaning_home_time_w fuel_home_time_w childcare_home_time_w other_childcare_home_time_w)
replace home_working_hrs=0 if home_working_hrs==. & startdate>=td(01oct2018)
winsor2 home_working_hrs, cuts(0 99) by(gt)

***Time worked by HH members**
foreach var of varlist working_hours_hh_*{
replace `var'=0 if `var'==. 
}

foreach var of varlist hh_home_production_*{
replace `var'=0 if `var'==. & startdate>=td(01oct2018)
}

foreach var of varlist working_days_hh_*{
replace `var'=0 if `var'==. 
}

gen total_wrk_hrs2= (working_hours_hh_1 *working_days_hh_1 + working_hours_hh_2*working_days_hh_2 + working_hours_hh_3*working_days_hh_3 + working_hours_hh_4*working_days_hh_4 + working_hours_hh_5*working_days_hh_5 + working_hours_hh_6*working_days_hh_6 + working_hours_hh_7*working_days_hh_7 + working_hours_hh_8*working_days_hh_8 + working_hours_hh_9*working_days_hh_9 + working_hours_hh_10*working_days_hh_10 + working_hours_hh_11*working_days_hh_11 + working_hours_hh_12*working_days_hh_13 + working_hours_hh_13*working_days_hh_13 + working_hours_hh_14*working_days_hh_14 + working_hours_hh_15*working_days_hh_15 + working_hours_hh_16*working_days_hh_16)
gen total_chores_hrs= (hh_home_production_1 + hh_home_production_2 + hh_home_production_3 + hh_home_production_4 + hh_home_production_5 + hh_home_production_6 + hh_home_production_7 + hh_home_production_8 + hh_home_production_9 + hh_home_production_10 + hh_home_production_11 + hh_home_production_12 + hh_home_production_13 + hh_home_production_14 + hh_home_production_15 + hh_home_production_16)
gen hh_work=total_chores_hrs+total_wrk_hrs2

winsor2 total_wrk_hrs2, cuts(0 99) by(gt)
winsor2 total_chores_hrs, cuts(0 99) by(gt)
winsor2 hh_work, cuts(0 99) by(gt)

***children education***
egen fulltime_school=rowtotal( fulltime_school_hh_1 fulltime_school_hh_2 fulltime_school_hh_3 fulltime_school_hh_4 fulltime_school_hh_5 fulltime_school_hh_6 fulltime_school_hh_7 fulltime_school_hh_8 fulltime_school_hh_9 fulltime_school_hh_10 fulltime_school_hh_11 fulltime_school_hh_12 fulltime_school_hh_13 fulltime_school_hh_14 fulltime_school_hh_15 fulltime_school_hh_16)
gen fulltime_school_hh=0 if fulltime_school==0
replace fulltime_school_hh=1 if inlist( fulltime_school,1,2,3,4,5,6)

	*Measures of business assets
			capture drop new_biz_assets
			foreach var of varlist inventory_amount goods_amount cash_amount premise_new ///
			land_new furniture_new equipment_machinery_new vehicules_new amount_maintanance{
				replace `var'=0 if `var'==.
				replace `var'=. if `var'==9999 | `var'==999 | `var'==1111 | `var'==9998 | `var'==998 | `var'==111
			}
			egen new_biz_assets=rowtotal(inventory_amount goods_amount cash_amount ///
			premise_new land_new furniture_new equipment_machinery_new vehicules_new amount_maintanance)

			foreach var of varlist inventory_amount goods_amount cash_amount premise_new land_new furniture_new equipment_machinery_new vehicules_new amount_maintanance{
				replace new_biz_assets=. if `var'==.
			}

			capture drop all_biz_assets
			egen all_biz_assets=rowtotal(inventory_amount goods_amount cash_amount ///
			premise_new land_new furniture_new equipment_machinery_new vehicules_new ///
			amount_maintanance premise land furniture equipment_machinery vehicules)
			replace monthly_expenses=0 if monthly_expenses==.
			replace inventory_amount=0 if inventory_yesno!=3 & inventory_amount==.

			capture drop e_other_income
			*gen e_other_income=rent_inc+other_inc+govt_inc+transfers_inc

	*Reformating date variable for external loans*
		foreach var of varlist rosca_startdate_1 rosca_startdate_2 rosca_startdate_3 rosca_startdate_4 familyloan_when_received_1 familyloan_when_received_2 familyloan_when_received_3 familyloan_when_received_4 familyloan_when_received_5 familyloan_when_received_6 familyloan_when_received_7 familyloan_when_received_8 loan_when_received_1 loan_when_received_2 loan_when_received_3 loan_when_received_4{
			format `var' %tdNN/DD/CCYY
		}
	
		gen match=0
		replace match=1 if match_amount==1 & abs(date_difference)<=60
		replace match=1 if match_amount==1 & abs(date_difference_2)<=60
		replace match=1 if match_amount==1 & abs(date_difference_3)<=60
		replace match=1 if match_amount==1 & abs(date_difference_4)<=60

		tab match treatment, col

		gen new_formal_loan_1=loan_amount_1 if match==0
		gen new_formal_loan_2=loan_amount_2 if match==0
		gen new_formal_loan_3=loan_amount_3 if match==0
		gen new_formal_loan_4=loan_amount_4 if match==0

		foreach var of varlist new_formal_loan_1 new_formal_loan_2 new_formal_loan_3 new_formal_loan_4 rosca_installements_* familyloan_amount_* loan_amount_1 loan_amount_2 loan_amount_3 loan_amount_4{
			replace `var'=. if `var'==9999 | `var'==999 | `var'==1111 | `var'==9998 | `var'==998 | `var'==111
		}

		foreach var of varlist new_formal_loan_1 new_formal_loan_2 new_formal_loan_3 new_formal_loan_4 loan_amount_1 loan_amount_2 loan_amount_3 loan_amount_4 {
			replace `var'=0 if `var'==.
		} 

		foreach num of numlist 1/4 {
			capture drop rosca_installements_m_`num'
			gen rosca_instlmts_m_`num'=. if rosca_install_freq_`num'!=3
			replace rosca_instlmts_m_`num'=rosca_installements_`num' if rosca_install_freq_`num'==2
			replace rosca_instlmts_m_`num'=4*rosca_installements_`num' if rosca_install_freq_`num'==1


			replace loan_use_`num'=0 if loan_use_`num'==.
			capture drop loan_amount_biz_`num'
			gen loan_amount_biz_`num'=new_formal_loan_`num'*(loan_use_`num'==1)
		}

		foreach num of numlist 1/8 {
			replace familyloan_use_`num'=0 if familyloan_use_`num'==.
			capture drop familyloan_amount_biz_`num'
			gen famloan_amount_biz_`num'=familyloan_amount_`num'*(familyloan_use_`num'==1)
		}

		foreach var in new_formal_loan loan_amount_biz rosca_amount rosca_instlmts_m familyloan_amount famloan_amount_biz loan_amount {
			capture drop `var'
			egen `var'=rowtotal(`var'_1 `var'_2 `var'_3 `var'_4)
			capture drop `var'_f
			winsor2 `var' if treatment!=. & female==1, cuts(0 95) by(treatment) suffix(_f)
			capture drop `var'_m
			winsor2 `var' if treatment!=. & female==0, cuts(0 95) by(treatment) suffix(_m)
			capture drop `var'_w
			egen `var'_w=rowtotal(`var'_f `var'_m)
			drop `var'_f `var'_m
			capture drop ihs_`var'_w
			gen ihs_`var'_w=log(`var'_w+sqrt(1+`var'_w^2))
		}

			capture drop total_amount total_amount_all
			gen total_amount=new_formal_loan_w+familyloan_amount_w
			gen total_amount_all=loan_amount_w+familyloan_amount_w

			capture drop loan_amount_other_w
			gen loan_amount_other_w=new_formal_loan_w-loan_amount_biz_w

			capture drop familyloan_amount_other_w
			gen famloan_amount_other_w=familyloan_amount_w-famloan_amount_biz_w

			capture drop total_loan_amount_other_w
			gen total_loan_amount_other_w=loan_amount_other_w+famloan_amount_other_w

			global expense fin_exp_water fin_exp_transport fin_exp_tax fin_exp_resale fin_exp_rent fin_exp_marketfees fin_exp_maintain  fin_exp_interest fin_exp_input fin_exp_fuel fin_exp_fixed_other fin_exp_fixed fin_exp_equip fin_exp_electricity fin_exp_commun fin_exp_bribes fin_computerize


			foreach var of global expense {
				replace `var'=0 if `var'==.
				capture drop `var'_f
				winsor2 `var' if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
				capture drop `var'_m
				winsor2 `var' if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
				capture drop `var'_w
				egen `var'_w=rowtotal(`var'_f `var'_m)
				drop `var'_f `var'_m
			}

		global  g_hh_year clothing_expenses footwear_expenses mobile_expenses other_large_expenses
		global  g_hh_quarter events_expenses electronics_expenses furniture_expenses vehicles_expenses health_expenses othereducation_expenses
		global  g_hh_month water_expenses personalitems_expenses personalservices_expenses cleaning_expenses transportation_expenses communication_expenses entertainment_expenses other_expenses hhservices_expenses contributions_expenses
		global  g_hh_week bread_expenses vegetables_expenses fruits_expenses chicken_expenses dairy_expenses tea_expenses potablewater_expenses
		global consumption $g_hh_year $g_hh_quarter $g_hh_month $g_hh_week
		
		foreach var of global consumption {
			replace `var'=0 if `var'==.
			capture drop `var'_f
			winsor2 `var' if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
			capture drop `var'_m
			winsor2 `var' if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
			capture drop `var'_w
			egen `var'_w=rowtotal(`var'_f `var'_m)
			drop `var'_f `var'_m
		}

		capture drop hh_total_2w
		gen hh_total_2w=0
		foreach var of global g_hh_year  {
			replace hh_total_2w=hh_total_2w+`var'_w/12
		}
		foreach var of global  g_hh_quarter {
			replace hh_total_2w=hh_total_2w+`var'_w/3
		}
		foreach var of global  g_hh_month  {
			replace hh_total_2w=hh_total_2w+`var'_w
		}
		foreach var of global  g_hh_week {
			replace hh_total_2w=hh_total_2w+`var'_w*4
		}


		global asset_list_new inventory_amount goods_amount cash_amount premise_new land_new furniture_new equipment_machinery_new vehicules_new amount_maintanance     
		global asset_list inventory_amount goods_amount cash_amount premise_new land_new furniture_new equipment_machinery_new vehicules_new amount_maintanance premise land furniture equipment_machinery vehicules

		foreach var of global asset_list {
			replace `var'=0 if `var'==.
			capture drop `var'_f
			winsor2 `var' if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
			capture drop `var'_m
			winsor2 `var' if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
			capture drop `var'_w
			egen `var'_w=rowtotal(`var'_f `var'_m)
			drop `var'_f `var'_m
		}

		capture drop asset_new_2w
		gen asset_new_2w=0
		foreach var of global asset_list_new  {
			replace asset_new_2w=asset_new_2w+`var'_w
		}

		capture drop asset_2w
		gen asset_2w=0
		foreach var of global asset_list  {
			replace asset_2w=asset_2w+`var'_w
		}

	rename e_weekly_hours_w e_weekly_hours_wage
	rename  e_weekly_hours_s e_weekly_hours_self

*Winsorizing variables
ren other_income e_other_income
foreach var in  amount_received inventory_amount cash_amount  goods_amount new_biz_assets all_biz_assets /// 
monthly_profits monthly_expenses monthly_revenue e_other_income wage_inc  loan_amount_biz /// 
rosca_amount rosca_instlmts_m familyloan_amount famloan_amount_biz ///
e_weekly_hours_self e_weekly_hours_wage  home_working_hrs saved_amount e_weekly_hours{
capture drop `var'_f
winsor2 `var' if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
capture drop `var'_m
winsor2 `var' if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
capture drop `var'_w
egen `var'_w=rowtotal(`var'_f `var'_m)
drop `var'_f `var'_m
}



gen wage_earner=e_weekly_hours_wage_w>0
gen self_employed=e_weekly_hours_self_w>0
gen self_and_wage=wage_earner*self_employed


capture drop e_total_income_w
foreach var of varlist monthly_profits_w transfers_inc_w rent_inc_w govt_inc_w other_inc_w wage_inc_w {
replace `var'=. if `var'==9999 | `var'==999 | `var'==1111 | `var'==9998 | `var'==998 | `var'==111
}
capture drop e_other_income
g e_other_income=rent_inc+other_inc+govt_inc+transfers_inc
g e_total_income_w=monthly_profits_w+wage_inc_w+e_other_income_w

capture drop ihs_e_total_income_w
gen ihs_e_total_income_w = ln(e_total_income_w+ sqrt(e_total_income_w^2+1))


capture drop poor
gen poor=e_total_income_w<690




capture drop net_e_total_income_w
gen net_e_total_income_w=e_total_income_w-0.024*Loan*amount_received_w

capture drop e_non_biz_income_w
gen e_non_biz_income_w=e_total_income_w-monthly_profits_w


*Assets*
foreach var of varlist premise_new_w land_new_w furniture_new_w equipment_machinery_new_w vehicules_new_w{
gen c_`var'=`var'
replace c_`var'=. if e_hasbiz==0
}

foreach var of varlist assets_new_1 assets_new_2 assets_new_3 assets_new_4 assets_new_5 assets_new_6 assets_new_7{
replace `var'=0 if `var'==.
}


*******Other financing*****
gen other_loan= amount_received_w if treatment==0
replace other_loan=0 if other_loan==.
replace new_formal_loan_w= new_formal_loan_w+other_loan
replace loan_amount_w=loan_amount_w+other_loan
capture drop total_amount total_amount_all
gen total_amount=new_formal_loan_w+familyloan_amount_w
gen total_amount_all=loan_amount_w+familyloan_amount_w


gen start_time=dofc(starttime)
format start_time %tdNN/DD/CCYY

foreach var of varlist rosca_startdate_1 rosca_startdate_2 rosca_startdate_3 rosca_startdate_4 rosca_enddate_1 rosca_enddate_2 rosca_enddate_3 rosca_enddate_4{
format `var' %tdNN/CCYY
}

forvalue i=1/4{
gen n_rosca_installements_`i'=rosca_installements_`i'*4 if rosca_install_freq_`i'==1
}

forvalue i=1/4{
replace n_rosca_installements_`i'=rosca_installements_`i' if rosca_install_freq_`i'!=1
}

gen num_rosca_months_1=(rosca_enddate_1-rosca_startdate_1)*12/365
gen num_rosca_months_2=(rosca_enddate_2-rosca_startdate_2)*12/365
gen num_rosca_months_3=(rosca_enddate_3-rosca_startdate_3)*12/365
gen num_rosca_months_4=(rosca_enddate_4-rosca_startdate_4)*12/365

gen rosca_month_1=(start_time-rosca_startdate_1)*12/365
gen rosca_month_2=(start_time-rosca_startdate_2)*12/365
gen rosca_month_3=(start_time-rosca_startdate_3)*12/365
gen rosca_month_4=(start_time-rosca_startdate_4)*12/365

gen remain_rosca_month_1=num_rosca_months_1-rosca_month_1
gen remain_rosca_month_2=num_rosca_months_2-rosca_month_2
gen remain_rosca_month_3=num_rosca_months_3-rosca_month_3
gen remain_rosca_month_4=num_rosca_months_4-rosca_month_4

forvalues i=1/4{
gen rosca_month_toreceive_`i'=(receive_rosca_when_`i'-rosca_startdate_`i')*12/365
}

forvalues i=1/4{
gen remain_save_months_`i'=num_rosca_months_`i'-rosca_month_toreceive_`i'
replace remain_save_months_`i'=0 if remain_save_months_`i'<0
}


foreach var of varlist num_rosca_months_1 num_rosca_months_2 num_rosca_months_3 num_rosca_months_4 rosca_month_1 rosca_month_2 rosca_month_3 rosca_month_4 remain_rosca_month_1 remain_rosca_month_2 remain_rosca_month_3 remain_rosca_month_4 rosca_month_toreceive_1 rosca_month_toreceive_2 rosca_month_toreceive_3 rosca_month_toreceive_4 remain_save_months_1 remain_save_months_2 remain_save_months_3 remain_save_months_4{
replace `var'=round(`var')
}

forvalues i=1/4{
gen rosca_savings_`i'=n_rosca_installements_`i'* remain_save_months_`i' if received_rosca_yesno_`i'==0
replace rosca_savings_`i'=0 if rosca_savings_`i'==. | rosca_savings_`i'<0
winsor2 rosca_savings_`i' if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
winsor2 rosca_savings_`i' if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
egen rosca_savings_`i'_w=rowtotal(rosca_savings_`i'_f rosca_savings_`i'_m)
drop rosca_savings_`i'_f rosca_savings_`i'_m
}

gen rosca_savings= rosca_savings_1_w +rosca_savings_2_w +rosca_savings_3_w +rosca_savings_4_w

forvalues i=1/4{
gen rosca_credit_`i'=n_rosca_installements_`i'* remain_rosca_month_`i' if received_rosca_yesno_`i'==1
replace rosca_credit_`i'=0 if rosca_credit_`i'==. | rosca_credit_`i'<0
winsor2 rosca_credit_`i' if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
winsor2 rosca_credit_`i' if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
egen rosca_credit_`i'_w=rowtotal(rosca_credit_`i'_f rosca_credit_`i'_m)
drop rosca_credit_`i'_f rosca_credit_`i'_m
}

gen rosca_credit= rosca_credit_1_w +rosca_credit_2_w +rosca_credit_3_w +rosca_credit_4_w


foreach var of varlist new_formal_loan_w familyloan_amount_w rosca_savings rosca_credit saved_amount_w loan_amount_w{ 
replace `var'=0 if `var'==.
sum `var' if treatment==0 & gender==0
scalar define `var'_ctrl_m_m=r(mean)
scalar define `var'_ctrl_sd_m=r(sd)
sum `var' if treatment==0 & gender==1
scalar define `var'_ctrl_m_f=r(mean)
scalar define `var'_ctrl_sd_f=r(sd)
gen m_`var'=`var'_ctrl_m_m
replace m_`var'= `var'_ctrl_m_f if gender==1
gen sd_`var'=`var'_ctrl_sd_m
replace sd_`var'= `var'_ctrl_sd_f if gender==1
gen std_`var' = (`var' - m_`var') / sd_`var'
}

egen loan_index=rowtotal(std_new_formal_loan_w std_familyloan_amount_w std_rosca_savings std_rosca_credit std_saved_amount_w)
sum loan_index if treatment==0 & gender==1
scalar define loan_index_mean_f=r(mean)
scalar define loan_index_sd_f=r(sd)
sum loan_index if treatment==0 & gender==0
scalar define loan_index_mean_m=r(mean)
scalar define loan_index_sd_m=r(sd)
gen m_loan_index= loan_index_mean_f
replace m_loan_index= loan_index_mean_m if gender==0
gen sd_loan_index= loan_index_sd_f
replace sd_loan_index= loan_index_sd_m if gender==0
gen std_loan_index = ( loan_index - m_loan_index ) / sd_loan_index

foreach var of varlist new_formal_loan_w familyloan_amount_w total_amount rosca_savings rosca_credit saved_amount_w loan_amount_w{
replace `var'=. if `var'==9999 | `var'==999 | `var'==1111 | `var'==9998 | `var'==998 | `var'==111
}

gen any_loan=0
replace any_loan=1 if inrange(total_amount_all,10,999999) | family_loan_yesno==1 | inrange(rosca_credit,10,99999)
gen total_outside_loans=total_amount+rosca_credit
gen total_ouside_loans_2=total_amount_all+rosca_credit
gen experiment_loans=amount_received
replace experiment_loans=0 if inlist(treatment,0,2,3)
gen total_external_loans=total_outside_loans+experiment_loans
gen total_external_loans_2=total_ouside_loans_2+experiment_loans


*********For Appendix*********
egen loan_index_2=rowtotal(std_new_formal_loan_w std_familyloan_amount_w std_rosca_savings std_rosca_credit std_saved_amount_w std_loan_amount_w)
sum loan_index_2 if treatment==0 & gender==1
scalar define loan_index_mean_f_2=r(mean)
scalar define loan_index_sd_f_2=r(sd)
sum loan_index_2 if treatment==0 & gender==0
scalar define loan_index_mean_m_2=r(mean)
scalar define loan_index_sd_m_2=r(sd)
gen m_loan_index_2= loan_index_mean_f_2
replace m_loan_index_2= loan_index_mean_m_2 if gender==0
gen sd_loan_index_2= loan_index_sd_f_2
replace sd_loan_index_2= loan_index_sd_m_2 if gender==0
gen std_loan_index_2 = ( loan_index_2 - m_loan_index_2 ) / sd_loan_index_2


*Business activity
******************************************************************************** 
foreach var of varlist e_hasbiz new_biz_assets_w monthly_revenue_w monthly_expenses_w monthly_profits_w{
sum `var' if treatment==0 & gender==0
scalar define `var'_control_m_m=r(mean)
scalar define `var'_control_sd_m=r(sd)
sum `var' if treatment==0 & gender==1
scalar define `var'_control_m_f=r(mean)
scalar define `var'_control_sd_f=r(sd)
gen m_`var'=`var'_control_m_m
replace m_`var'= `var'_control_m_f if gender==1
gen sd_`var'=`var'_control_sd_m
replace sd_`var'= `var'_control_sd_f if gender==1
gen std_`var' = (`var' - m_`var') / sd_`var'
}

egen biz_index=rowtotal(std_e_hasbiz std_new_biz_assets_w std_monthly_revenue_w std_monthly_expenses_w std_monthly_profits_w)
sum biz_index if treatment==0 & gender==1
scalar define biz_index_mean_f=r(mean)
scalar define biz_index_sd_f=r(sd)
sum biz_index if treatment==0 & gender==0
scalar define biz_index_mean_m=r(mean)
scalar define biz_index_sd_m=r(sd)
gen m_biz_index= biz_index_mean_f
replace m_biz_index= biz_index_mean_m if gender==0
gen sd_biz_index= biz_index_sd_f
replace sd_biz_index= biz_index_sd_m if gender==0
gen std_biz_index = ( biz_index - m_biz_index ) / sd_biz_index

foreach var of varlist new_biz_assets_w monthly_revenue_w monthly_expenses_w monthly_profits_w{
replace `var'=. if `var'==9999 | `var'==999 | `var'==1111 | `var'==9998 | `var'==998 | `var'==111
}

capture drop total_amount
gen total_amount=new_formal_loan_w+familyloan_amount_w
gen total_outside_finance= total_amount+ rosca_amount_w
gen new_amount_received=amount_received
replace new_amount_received=0 if treatment==0 
gen total_external_funding=new_amount_received+total_outside_finance


**no project**

capture drop no_project
gen no_project=(why_no_project!=.)

capture drop failed_project
gen failed_project=0 
replace failed_project=1 if why_no_project==1

capture drop different_use
gen different_use=0 
replace different_use=1 if why_no_project==2

capture drop insufficient_fund
gen insufficient_fund=0 
replace insufficient_fund=1 if why_no_project==3

gen project=.
replace project=1 if e_hasbiz==1
replace project=2 if why_no_project==1
replace project=3 if why_no_project==2
replace project=4 if why_no_project==3
label define project 1"Has a business" 2"Failed project" 3"Other use of funds" 4"Insufficient funds"
label values project project


****Time use***

foreach var of varlist e_weekly_hours_wage_w e_weekly_hours_self_w agri_home_time_w{
sum `var' if treatment==0 & gender==0
scalar define `var'_ctrl_m_m=r(mean)
scalar define `var'_ctrl_sd_m=r(sd)
sum `var' if treatment==0 & gender==1
scalar define `var'_ctrl_m_f=r(mean)
scalar define `var'_ctrl_sd_f=r(sd)
gen m_`var'=`var'_ctrl_m_m
replace m_`var'= `var'_ctrl_m_f if gender==1
gen sd_`var'=`var'_ctrl_sd_m
replace sd_`var'= `var'_ctrl_sd_f if gender==1
gen std_`var' = (`var' - m_`var') / sd_`var'
}

egen time_index=rowtotal(std_e_weekly_hours_wage_w std_e_weekly_hours_self_w std_agri_home_time_w)
sum time_index if treatment==0 & gender==1
scalar define time_index_mean_f=r(mean)
scalar define time_index_sd_f=r(sd)
sum time_index if treatment==0 & gender==0
scalar define time_index_mean_m=r(mean)
scalar define time_index_sd_m=r(sd)
gen m_time_index= time_index_mean_f
replace m_time_index= time_index_mean_m if gender==0
gen sd_time_index= time_index_sd_f
replace sd_time_index= time_index_sd_m if gender==0
gen std_time_index = ( time_index - m_time_index ) / sd_time_index


foreach var of varlist e_weekly_hours_wage_w e_weekly_hours_w agri_home_time_w  childcare_home_time_w maintenance_home_time_w cleaning_home_time_w fuel_home_time_w other_childcare_home_time_w{
replace `var'=. if `var'==9999 | `var'==999 | `var'==1111 | `var'==9998 | `var'==998 | `var'==111
}

egen chores_home_time_w=rowtotal( maintenance_home_time_w cleaning_home_time_w fuel_home_time_w other_childcare_home_time_w)



gen self_non_wage=self_employed*(1-wage_earner)
gen non_self_wage=(1-self_employed)*wage_earner
gen non_self_non_wage=(1-self_employed)*(1-wage_earner)



 capture drop no_work
 gen no_work=1-income_sources_1*income_sources_2*income_sources_3

 
 
 * Income table
******************************************************************************** 
gen other_fam_transfers=other_inc_w if other_income_source==1
replace other_fam_transfers=0 if other_fam_transfers==.
gen other_labor_inc=other_inc_w if inlist(other_income_source,2,4)
replace other_labor_inc=0 if other_labor_inc==.
gen other_gov_inc=other_inc_w if other_income_source==3
replace other_gov_inc=0 if other_gov_inc==.


foreach var of varlist other_labor_inc other_fam_transfers other_gov_inc{
replace `var'=0 if `var'==.
replace `var'=. if `var'==9999 | `var'==999 | `var'==1111 | `var'==9998 | `var'==998 | `var'==111
}

gen labor_income=monthly_profits_w+wage_inc_w+other_labor_inc
gen family_transfers=other_fam_transfers+transfers_inc_w
gen gov_transfers=other_gov_inc+govt_inc_w

*physical health*
recode health_description (1=5) (2=4) (4=2) (5=1)

*hh consumption*
foreach var of varlist clothing_expenses footwear_expenses mobile_expenses other_large_expenses events_expenses electronics_expenses furniture_expenses vehicles_expenses health_expenses othereducation_expenses water_expenses personalitems_expenses personalservices_expenses cleaning_expenses transportation_expenses communication_expenses entertainment_expenses other_expenses hhservices_expenses contributions_expenses bread_expenses vegetables_expenses fruits_expenses chicken_expenses dairy_expenses tea_expenses potablewater_expenses{
replace `var'=. if `var'==9999 | `var'==999 | `var'==1111 | `var'==9998 | `var'==998 | `var'==111
capture drop `var'_f
winsor2 `var' if treatment!=. & female==1, cuts(0 99) by(treatment) suffix(_f)
capture drop `var'_m
winsor2 `var' if treatment!=. & female==0, cuts(0 99) by(treatment) suffix(_m)
capture drop `var'_w
egen `var'_w=rowtotal(`var'_f `var'_m)
replace `var'_w=. if `var'==.
drop `var'_f `var'_m
}

foreach var of varlist clothing_expenses_w footwear_expenses_w mobile_expenses_w other_large_expenses_w{
gen new_`var'=`var'/12
replace new_`var'=. if `var'==.
}

foreach var of varlist events_expenses_w electronics_expenses_w furniture_expenses_w vehicles_expenses_w health_expenses_w othereducation_expenses_w{
gen new_`var'=`var'/3
replace new_`var'=. if `var'==.
}

foreach var of varlist bread_expenses_w vegetables_expenses_w fruits_expenses_w chicken_expenses_w dairy_expenses_w tea_expenses_w potablewater_expenses_w{
gen new_`var'=`var'*4
replace new_`var'=. if `var'==.
}

drop hh_total_w
egen hh_total_w=rowtotal (new_clothing_expenses_w new_footwear_expenses_w new_mobile_expenses_w new_other_large_expenses_w new_events_expenses_w new_electronics_expenses_w new_furniture_expenses_w new_vehicles_expenses_w new_health_expenses_w new_othereducation_expenses_w new_bread_expenses_w new_vegetables_expenses_w new_fruits_expenses_w new_chicken_expenses_w new_dairy_expenses_w new_tea_expenses_w new_potablewater_expenses_w water_expenses_w personalitems_expenses_w personalservices_expenses_w transportation_expenses_w cleaning_expenses_w communication_expenses_w entertainment_expenses_w other_expenses_w hhservices_expenses_w contributions_expenses_w)
foreach var of varlist new_clothing_expenses_w new_footwear_expenses_w new_mobile_expenses_w new_other_large_expenses_w new_events_expenses_w new_electronics_expenses_w new_furniture_expenses_w new_vehicles_expenses_w new_health_expenses_w new_othereducation_expenses_w new_bread_expenses_w new_vegetables_expenses_w new_fruits_expenses_w new_chicken_expenses_w new_dairy_expenses_w new_tea_expenses_w new_potablewater_expenses_w water_expenses_w personalitems_expenses_w personalservices_expenses_w transportation_expenses_w cleaning_expenses_w communication_expenses_w entertainment_expenses_w other_expenses_w hhservices_expenses_w contributions_expenses_w{
replace hh_total_w=. if `var'==.
}





*mental health*
drop std_worried_yesno_f std_worried_yesno_m std_worried_yesno std_depressed_f std_depressed_m std_depressed std_worried_f std_worried_m std_worried std_mental_health_f std_mental_health_m std_mental_health

foreach var of varlist worried_yesno depressed worried_days{
replace `var'=0 if `var'==.
sum `var' if treatment==0 & gender==0
scalar define `var'_ctrl_m_m=r(mean)
scalar define `var'_ctrl_sd_m=r(sd)
sum `var' if treatment==0 & gender==1
scalar define `var'_ctrl_m_f=r(mean)
scalar define `var'_ctrl_sd_f=r(sd)
gen m_`var'=`var'_ctrl_m_m
replace m_`var'= `var'_ctrl_m_f if gender==1
gen sd_`var'=`var'_ctrl_sd_m
replace sd_`var'= `var'_ctrl_sd_f if gender==1
gen std_`var' = (`var' - m_`var') / sd_`var'

}

egen mental_health_index=rowtotal(std_worried_yesno std_depressed std_worried_days)
sum mental_health_index if treatment==0 & gender==1
scalar define mental_health_index_mean_f=r(mean)
scalar define mental_health_index_sd_f=r(sd)
sum mental_health_index if treatment==0 & gender==0
scalar define mental_health_index_mean_m=r(mean)
scalar define mental_health_index_sd_m=r(sd)
gen m_mental_health_index= mental_health_index_mean_f
replace m_mental_health_index= mental_health_index_mean_m if gender==0
gen sd_mental_health_index= mental_health_index_sd_f
replace sd_mental_health_index= mental_health_index_sd_m if gender==0
gen std_mental_health = (mental_health_index - m_mental_health_index ) / sd_mental_health_index



*consumption*
egen hh_assets=rowtotal(new_mobile_expenses_w new_electronics_expenses_w new_furniture_expenses_w new_vehicles_expenses_w)
replace hh_assets=. if new_mobile_expenses_w==. & new_electronics_expenses_w==. & new_furniture_expenses_w==. & new_vehicles_expenses_w==.

egen hh_leisure=rowtotal(new_events_expenses entertainment_expenses)
replace hh_leisure=. if new_events_expenses==. & entertainment_expenses==.

egen hh_food=rowtotal(new_bread_expenses_w new_vegetables_expenses_w new_fruits_expenses_w new_chicken_expenses_w new_dairy_expenses_w new_tea_expenses_w new_potablewater_expenses_w)
replace hh_food=. if new_bread_expenses_w==. & new_vegetables_expenses_w==. & new_fruits_expenses_w==. & new_chicken_expenses_w==. & new_dairy_expenses_w==. & new_tea_expenses_w==. & new_potablewater_expenses_w==. 
replace hh_food=. if new_bread_expenses_w==. & new_vegetables_expenses_w==. & new_fruits_expenses_w==. & new_chicken_expenses_w==. & new_dairy_expenses_w==. & new_tea_expenses_w==. & new_potablewater_expenses_w==0 
replace hh_food=. if hh_food==0

egen hh_other=rowtotal(new_other_large_expenses_w other_expenses_w contributions_expenses_w)
replace hh_other=. if new_other_large_expenses_w==. & other_expenses_w==. & contributions_expenses_w==.

egen hh_personal=rowtotal(personalitems_expenses_w  personalservices_expenses_w)
replace hh_personal=. if personalitems_expenses_w==. & personalservices_expenses_w==.


foreach var of varlist landline_wealth desktop_wealth laptop_wealth mobile_phone_wealth satellite_dish_wealth washing_machine_wealth colored_tv_wealth iron_wealth fridge_wealth freezer_wealth gas_oven_wealth water_heater_wealth private_car_wealth toc_toc_wealth bicycle_wealth air_conditioner_wealth fan_wealth microwave_wealth sewing_wealth{
replace `var'=. if `var'==3
}

global consumption hh_assets hh_leisure hh_food hh_personal new_clothing_expenses_w new_footwear_expenses_w new_health_expenses_w new_othereducation_expenses_w water_expenses_w cleaning_expenses_w transportation_expenses_w communication_expenses_w hhservices_expenses_w hh_other

	
***Loan size**
cap label drop gender treatment_status
label define treatment_status 0 "Control" 1 "Loan" 2 "In-Kind Grant" 3 "Cash Grant"
label values treatment_status treatment_status
cap label drop gender
label define gender 0 "Male" 1"Female"
label values gender gender
gen amount_bin=.
replace amount_bin=0 if amount_received==0
replace amount_bin=1 if amount_received>0 & amount_received<1750
replace amount_bin=2 if amount_received>=1750 & amount_received<2250
replace amount_bin=3 if amount_received>=2250 & amount_received<2750
replace amount_bin=4 if amount_received>=2750
label define amount_bin 0"0" 1"<1750" 2"1750-2250" 3"2250-2750" 4">2750"
label values amount_bin amount_bin

	*Save 
		compress
		save "$rdata/AEJ_CKO_endline_edited.dta",replace

********************************************************************************
*****3) Baseline_Endline Edited 
********************************************************************************
use "$rdata/AEJ_CKO_baseline_edited.dta", clear
merge 1:1 u_key using "$rdata\AEJ_CKO_endline_edited.dta", force gen(combo)

*Keep only people who did an endline and we have baseline data on
keep if e_hasbiz!=. & b_agebase!=.

*1- business at baseline*
split date_received, p(/)

replace date_received3="2016" if date_received3=="" & inlist(round,101,102,103)
replace date_received3="2017" if date_received3=="" & inlist(round,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119)
replace date_received3="2018" if date_received3=="" & inlist(round,120,121,122,123,124,125)
replace date_received3="2016" if date_received3=="" & inlist(round,201,202,203,204)
replace date_received3="2017" if date_received3=="" & inlist(round,205,206,207,208,209,210,211)
replace date_received3="2018" if date_received3=="" & inlist(round,212,213,214)
replace date_received3="2016" if date_received3=="" & inlist(round,301,302)
replace date_received3="2017" if date_received3=="" & inlist(round,303,304,305,306,307,308,309,310,311,312,313,314)
replace date_received3="2018" if date_received3=="" & inlist(round,315,316,317,318,319,320,321,322,323)

destring date_received3, gen(year_received)
*business is old if it began operation before the year where loan/grant is received*
gen business_newold=0 if year_received<=begin_operations & e_hasbiz==1 
replace business_newold=1 if year_received>begin_operations & e_hasbiz==1



**variable that takes 1 if respondent said they had a business in the baseline survey**
gen has_biz_baseline= 0
replace has_biz_baseline= 1 if inlist(b_emp_type,"3","4")
**adding in people whose business began before loan/grant was recieved..
*I'm assuming here that the endline data is more accurate since it was collected by us and not the NGOs*
*replace has_biz_baseline=1 if business_newold==1 & has_biz_baseline==0

*2-Family demands*
gen family_demands=0
replace family_demands=1 if b_profit_family==1 | b_money_family_request==1 | b_success_req_money==1

gen hh_moneyshare=0 if b_money_family_request==0
replace hh_moneyshare=1 if b_money_family_request==1
gen success_moneyshare=0 if b_success_req_money==0
replace success_moneyshare=1 if b_success_req_money==1
gen profit_moneyshare=0 if b_profit_family==0
replace profit_moneyshare=1 if b_profit_family==1


*3-Risk aversion**
replace risk_yesno=. if risk_yesno==1111
replace risk_yesno=5 if risk_yesno==50

*5-Complimentary Financing*

gen any_borrowing=0
replace any_borrowing=1 if b_loan_fund==1

**6- Human capital*
*staff_yesno

*7-interest in entrepreneurship
gen interest_entrepreneur=0 if inlist(b_work_pref,2,3)
replace interest_entrepreneur=1 if b_work_pref==1

*8- Self control
replace b_save_regularly=. if b_save_regularly==99
replace b_save_regularly=0 if b_save_regularly==2

*9-has kids*
gen has_kids=0
replace has_kids=1 if b_child==1

*single parent*
gen single_parent=0 if has_kids==1
replace single_parent=1 if has_kids==1 & inlist(marital,3,4,5)

*10-Soft skills and technical skills training*
gen soft_skills_yesno=0
replace soft_skills_yesno=1 if business_courses==1 | business_training==1

gen technical_training=0
replace technical_training=1 if skills_yesno==1 | technical_assistance==1

gen any_training=0
replace any_training=1 if soft_skills_yesno==1 | technical_training==1

*11-Marital status*
gen ever_married=1
replace ever_married=0 if marital==1

*12-digit recall*
foreach var of varlist r1_answer r2_answer r3_answer r4_answer r5_answer r6_answer r7_answer r8_answer r9_answer {
replace `var'=. if `var'==3
}

egen digit_recall=rowtotal(r1_answer r2_answer r3_answer r4_answer r5_answer r6_answer r7_answer r8_answer r9_answer)
replace digit_recall=. if digit_recall==0

*13-discounting*
foreach var of varlist discounting1 discounting2 discounting3{
recode `var' (2=0)
}

*14-formality*
gen formal=0 if registration==0
replace formal=1 if registration==1

*business sectors*
rename sector_business_1 sector_services
rename sector_business_2 sector_retail
rename sector_business_3 sector_manufacturing
rename sector_business_4 sector_agriculture
rename sector_business_5 sector_wholesale


***********External pressures******************
foreach var of varlist b_profit_family b_money_family_request b_success_req_money b_machines_save_money{
recode `var' (2=0) (99=.)
}

replace b_house_mates=. if b_house_mates>15

gen b_married=b_marital==2


*Narrow external pressure*

foreach var of varlist b_profit_family b_money_family_request b_success_req_money b_machines_save_money {
replace `var'=0 if `var'==.
sum `var' if treatment==0 & gender==0
scalar define `var'_ctrl_m_m=r(mean)
scalar define `var'_ctrl_sd_m=r(sd)
sum `var' if treatment==0 & gender==1
scalar define `var'_ctrl_m_f=r(mean)
scalar define `var'_ctrl_sd_f=r(sd)
gen m_`var'=`var'_ctrl_m_m
replace m_`var'= `var'_ctrl_m_f if gender==1
gen sd_`var'=`var'_ctrl_sd_m
replace sd_`var'= `var'_ctrl_sd_f if gender==1
gen std_`var' = (`var' - m_`var') / sd_`var'
}

egen narrow_index=rowtotal(std_b_profit_family std_b_money_family_request std_b_success_req_money std_b_machines_save_money)
sum narrow_index if treatment==0 & gender==1
scalar define narrow_index_mean_f=r(mean)
scalar define narrow_index_sd_f=r(sd)
sum narrow_index if treatment==0 & gender==0
scalar define narrow_index_mean_m=r(mean)
scalar define narrow_index_sd_m=r(sd)
gen m_narrow_index= narrow_index_mean_f
replace m_narrow_index= narrow_index_mean_m if gender==0
gen sd_narrow_index= narrow_index_sd_f
replace sd_narrow_index= narrow_index_sd_m if gender==0
gen std_narrow_pressure = ( narrow_index - m_narrow_index ) / sd_narrow_index


*Broad external pressure*
foreach var of varlist b_house_mates b_married {
replace `var'=0 if `var'==.
sum `var' if treatment==0 & gender==0
scalar define `var'_ctrl_m_m=r(mean)
scalar define `var'_ctrl_sd_m=r(sd)
sum `var' if treatment==0 & gender==1
scalar define `var'_ctrl_m_f=r(mean)
scalar define `var'_ctrl_sd_f=r(sd)
gen m_`var'=`var'_ctrl_m_m
replace m_`var'= `var'_ctrl_m_f if gender==1
gen sd_`var'=`var'_ctrl_sd_m
replace sd_`var'= `var'_ctrl_sd_f if gender==1
gen std_`var' = (`var' - m_`var') / sd_`var'
}

egen broad_index=rowtotal(std_b_profit_family std_b_money_family_request std_b_success_req_money std_b_machines_save_money std_b_house_mates std_b_married)
sum broad_index if treatment==0 & gender==1
scalar define broad_index_mean_f=r(mean)
scalar define broad_index_sd_f=r(sd)
sum broad_index if treatment==0 & gender==0
scalar define broad_index_mean_m=r(mean)
scalar define broad_index_sd_m=r(sd)
gen m_broad_index= broad_index_mean_f
replace m_broad_index= broad_index_mean_m if gender==0
gen sd_broad_index= broad_index_sd_f
replace sd_broad_index= broad_index_sd_m if gender==0
gen std_broad_pressure = ( broad_index - m_broad_index ) / sd_broad_index



****PCA****
sum b_profit_family b_money_family_request b_success_req_money b_machines_save_money b_house_mates b_married if female ==1
capture drop b_broad_pressure
pca b_profit_family b_money_family_request b_success_req_money b_machines_save_money b_house_mates b_married 
rotate
predict b_broad_pressure



****Generating variables for lasso regressions

gen b_wife_low_ed=inrange(b_wife_edu,11,66)
gen b_wife_high_ed=inrange(b_wife_edu,77,88)
gen b_wife_NA=b_wife_edu==.

gen b_hus_low_ed=inrange(b_husband_edu,11,66)
gen b_hus_high_ed=inrange(b_husband_edu,77,88)
gen b_hus_NA=b_wife_edu==.
replace b_hus_NA=1 if b_husband_edu==9999

gen b_work_before_yes=b_work_before==1
gen b_work_before_no=b_work_before==2
gen b_work_before_NA=b_work_before==.

gen b_work_status_w=inlist(b_work_status,1,2)
gen b_work_status_nw=inlist(b_work_status,3,4)
gen b_work_status_NA=b_work_status==.
replace b_work_status_NA=. if b_work_status==99

gen b_work_days_lasso=b_work_days
replace b_work_days_lasso=0 if b_work_days==.
replace b_work_days_lasso=0 if b_work_days==9999

gen b_work_hrs_lasso=b_work_hrs
replace b_work_hrs_lasso=0 if b_work_hrs==.
replace b_work_hrs_lasso=0 if b_work_hrs==9999

gen b_work_days_lw_lasso=b_work_days_lastweek
replace b_work_days_lw_lasso=0 if b_work_days_lastweek==.
replace b_work_days_lw_lasso=0 if b_work_days_lastweek==9999


gen b_work_hrs_lw_lasso=b_work_hrs_lastweek
replace b_work_hrs_lw_lasso=0 if b_work_hrs_lastweek==.
replace b_work_hrs_lw_lasso=0 if b_work_hrs_lastweek==9999

gen b_work_inc_lm_las=b_work_income_lastmonth
replace b_work_inc_lm_las=0 if b_work_income_lastmonth==.
replace b_work_inc_lm_las=0 if b_work_income_lastmonth==9999

gen b_work_inc_las=b_work_income
replace b_work_inc_las=0 if b_work_income==.
replace b_work_inc_las=0 if b_work_income==9999



*****Generating quartile groups
gen group=treatment+female*10

*Top 25
gen top25=0
gen percentile5075=0
gen percentile2550=0
gen bottom25=0


foreach i in 0 1 2 3 10 11 12 13{
xtile top100_`i' = e_total_income if group==`i', nq(100)
replace top25=1 if inrange(top100_`i',75,100)
replace percentile5075=1 if inrange(top100_`i',50,75)
replace percentile2550=1 if inrange(top100_`i',25,50)
replace bottom25=1 if inrange(top100_`i',0,25)
drop top100_`i'
}
 
gen top25_female=top25
replace top25_female=. if female==0

gen top25_male=top25
replace top25_male=. if male==0

gen percentile5075_female=percentile5075
replace percentile5075_female=. if female==0

gen percentile5075_male=percentile5075
replace percentile5075_male=. if male==0
 
gen percentile2550_female=percentile2550
replace percentile2550_female=. if female==0

gen percentile2550_male=percentile2550
replace percentile2550_male=. if male==0


gen bottom25_female=bottom25
replace bottom25_female=. if female==0

gen bottom25_male=top25
replace bottom25_male=. if male==0


*Save 
	compress
	save "$rdata/AEJ_CKO_baseline_endline_edited.dta",replace
